package com.mcode.blog.exception;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.mcode.blog.vo.ErrorCode;
import com.mcode.blog.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.NoHandlerFoundException;

import java.util.Objects;

/**
 * 全局异常处理
 */
@Slf4j
@ControllerAdvice
public class GlobalException {

    /**
     * 处理所有不可知异常
     */
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result handleException(Exception e) {
        e.printStackTrace();
        System.out.println(e.getMessage());
        log.error("系统异常 {}", e.getMessage());
        return Result.fail(ErrorCode.SYSTEM_ERROR.getCode(), e.getMessage());
    }


    /**
     * 拦截404异常
     */
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(NoHandlerFoundException.class)
    @ResponseBody
    public Result handleNoHandlerFoundException(NoHandlerFoundException e) {
        return Result.fail(ErrorCode.REQUEST_404_ERROR.getCode(), e.getMessage());
    }


    /**
     * 系统错误异常
     */
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(SystemException.class)
    @ResponseBody
    public Result handleException(SystemException e) {
        String msg = e.getMsg();
        return Result.fail(ErrorCode.SYSTEM_ERROR.getCode(), msg);
    }


    /**
     * 拦截MybatisPlus异常
     */
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(MybatisPlusException.class)
    @ResponseBody
    public Result handleMybatisPlusException(MybatisPlusException e) {
        Integer code = ErrorCode.ASSERT_MYBATIS_ERROR.getCode();
        String msg = Objects.requireNonNull(e.getMessage());
        return Result.fail(code, msg);
    }


    /**
     * 拦截自定义抛出异常
     */
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(BaseException.class)
    @ResponseBody
    public Result handleException(BaseException e) {
        Integer code = e.getCode();
        String msg = e.getMsg();
        return Result.fail(code, msg);
    }

    /**
     * 拦截路径参数校验PATH
     */
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(MissingServletRequestParameterException.class)
    @ResponseBody
    public Result handlePathException(MissingServletRequestParameterException e) {
        Integer code = ErrorCode.PARAMS_ERROR.getCode();
        String msg   = Objects.requireNonNull(e.getMessage());
        return Result.fail(code, msg);
    }

    /**
     * 拦截JSON参数校验
     */
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseBody
    public Result handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        BindingResult bindingResult = e.getBindingResult();
        Integer code = ErrorCode.PARAMS_ERROR.getCode();
        String msg   = Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage();
        return Result.fail(code, msg);
    }

    /**
     * 拦截参数类型不正确
     */
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(HttpMessageNotReadableException.class)
    @ResponseBody
    public Result handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
        Integer code = ErrorCode.PARAMS_ERROR.getCode();
        String msg   = Objects.requireNonNull(e.getMessage());
        return Result.fail(code, msg.split(";")[0]);
    }

}
